<?php
declare (strict_types = 1);
namespace app\admin\controller;

class Config extends AdminBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Config;
        $this->validate = new \app\common\validate\Config;
    }
    public function index()
    {
        $where = [];
        // 自定义搜索
        if(method_exists($this, 'searchConditionHandle')){
            $this->searchConditionHandle($where);
        }
        // 排序字段
        $sort_by = $this->request->param('sort/s', $this->model->getPk());
        // 升序/降序
        $desc = $this->request->param('descending/s', 'true');
        // 分页容量
        $page_size = $this->request->param('page_size/d', 1000);
        // dump($where);exit;
        // 获取内容
        $result = $this->model->where($where)->order($this->order)->order($desc === 'true' ? $sort_by.' desc' : $sort_by)->paginate($page_size);
        return json($result);
    }
    // 设置搜索条件
    protected function searchConditionHandle(&$where)
    {
        $search = $this->request->param('search',null);
        if($search) {
            $search = json_decode($search, true);
            if(is_array($search) && sizeof($search) > 0) {
                if(array_key_exists('field', $search) && !empty($search['field'])){
                    ((array_key_exists('keyword', $search) && !empty($search['keyword'])) && $where[] = [$search['field'], 'like', '%'.$search['keyword'].'%']);
                } else {
                    ((array_key_exists('keyword', $search) && !empty($search['keyword'])) && $where[] = ['name|value|scope|remark','like', '%'.$search['keyword'].'%']);
                }
            }
        }
    }
}
